package tgc.edu.tms.service;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import tgc.edu.tms.custom.CommonService;
import tgc.edu.tms.entity.Schedule;
import tgc.edu.tms.entity.Schoolyear;
import tgc.edu.tms.entity.TbClass;
import tgc.edu.tms.entity.Teacher;
import tgc.edu.tms.repository.ScheduleRepository;
import tgc.edu.tms.repository.SchoolyearRepository;
import tgc.edu.tms.web.form.AgcsFrom;

@Service
public class ScheduleService extends CommonService<Schedule, Integer>{
	
	@Autowired
	private SchoolyearRepository schoolyearDAO;
	
	@Autowired
	private ScheduleRepository scheduleDAO;
	
	
	public List<String> findByTeacher(String username) {
		return scheduleDAO.findByTeacher(username);
		
	}
	
	public List<String> findAllKcmc(String name, String xq) {
		return scheduleDAO.findAllKcmc(name,xq);
	}

	public List<Schedule> findByTeacherAndKcmc(String name, String kcmc) {
		return scheduleDAO.findByTeacherAndKcmc(name,kcmc);
	}

	public List<Schedule> findByTeacherAndXnxq(String name, String xnxq) {
		return scheduleDAO.findByTeacherAndXnxq(name, xnxq);
	}

	@Transactional
	public void deleteByScyearId(Integer schoolyearID) {
		Optional<Schoolyear> schoolyear = schoolyearDAO.findById(schoolyearID);
		if(schoolyear.isPresent()) {
			scheduleDAO.deleteByXnxq(schoolyear.get().getXnxq());
		}
	}

	public List<Schedule> findByTeachers(Teacher teacher, AgcsFrom from) {
		Optional<Schoolyear> schoolyear = schoolyearDAO.findById(from.getSchoolyearID());
		if(schoolyear.isPresent()) {
			List<Schedule> ClassSche=scheduleDAO.findByTeacherAndXnxq(teacher.getName(),schoolyear.get().getXnxq());	
			return ClassSche;
		}
		return null;
	}

	public List<Schedule> findByClasses(List<TbClass> etbclass, AgcsFrom from) {
		Optional<Schoolyear> schoolyear = schoolyearDAO.findById(from.getSchoolyearID());
		if(schoolyear.isPresent()) {
			
			Specification<Schedule> spec = new Specification<Schedule>() {

				@Override
				public Predicate toPredicate(Root<Schedule> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					HashSet<Predicate> rules = new HashSet<>();
					
					for(int i=0;i<etbclass.size();i++) {
						TbClass tbClass = etbclass.get(i);
						rules.add(criteriaBuilder.like(root.get("tbclass"), "%"+tbClass.getName()+"%"));
					}
					
					return criteriaBuilder.or(rules.toArray(new Predicate[rules.size()]));
				}
			};
			List<Schedule> schedules=scheduleDAO.findAll(spec);
			return schedules;
		}
		return null;
	}

	public Integer findMaxId() {
		Integer findMaxId = scheduleDAO.findMaxId();
		if(findMaxId==null) {
			findMaxId=0;
		}
		return findMaxId;
	}

}
